import java.io.*;
import java.util.List;
import java.util.LinkedList;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.*;

//Reads alerts from a fast alert dump file (parsed by Snort)
public class LogReader {
	
	//Path to log file
	private String logFileName = ""; 
	private List<Alert> alertList = new LinkedList<Alert>(); 
	private List<Host> hostList = new LinkedList<Host>();
	
	public LogReader(String filename)
	{
		this.logFileName = filename;
	}
	
	public void readLog()
	{
		try 
        {
			BufferedReader br = new BufferedReader(new FileReader(logFileName));
			
            String currentLine;
            
            //For each alert, split the information and place it in a string array
            while ((currentLine = br.readLine()) != null) {

                String[] alertData = currentLine.split("\t");     
                
                int eventId = Integer.parseInt(alertData[3]);
                
                Date timeStamp = new Date();
             // Must have try catch when trying to convert string to Date
        		try
        		{        			
        			timeStamp = new SimpleDateFormat("DD-MM-YY hh:mm aa", Locale.ENGLISH).parse(alertData[0]);
        		}
        		catch (Exception e)
        		{	}
        		
        		long time = timeStamp.getTime();
        		
        		//Pad start with 0
        		String currentMilisecond = alertData[1];			
        		
        		//Get the rest of the Data
        		int miliseconds = Integer.parseInt(currentMilisecond);
        		int alertId = Integer.parseInt(alertData[3]);
        		String alertName = alertData[5]; 
        		String classification = alertData[6]; 
        		int priority = Integer.parseInt(alertData[7].split("Priority: ")[1]);
        		String protocol = alertData[8];
        		
        		String sourceIP = "";
        		int sourcePort = -1;
        		//2 cases: if there's a port number or there is not. It's better to do an 'if' statement check on whether or not alertData[9] has a ':' but I couldn't get that to work
        		try
        		{
        			sourceIP = alertData[9].split(":")[0];
        			sourcePort = Integer.parseInt(alertData[9].split(":")[1]); 
        		}
        		catch (Exception e)
        		{
        			sourceIP = alertData[9];
        		}
        		
        		String destinationIP = "";
        		int destinationPort = -1;
        		//2 cases: if there's a port number or there is not. It's better to do an 'if' statement check on whether or not alertData[10] has a ':' but I couldn't get that to work
        		try
        		{
        			destinationIP = alertData[11].split(":")[0]; 
        			destinationPort = Integer.parseInt(alertData[11].split(":")[1]);
        		}
        		catch (Exception e){
        			destinationIP = alertData[11];
        		}
                
                
                
                Alert newAlert = new Alert(eventId, time, miliseconds, alertId, alertName, classification, sourceIP, destinationIP);
                alertList.add(newAlert);
                System.out.println(currentLine);
            }
            
            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        } 
	}
	
	public void saveAlertLog(String filename)
	{
		try
		{
			FileOutputStream fout = new FileOutputStream(filename);
	        ObjectOutputStream oos = new ObjectOutputStream(fout);
	        oos.writeObject(alertList);
		}
		catch (Exception e)
		{			
			System.out.println(e.getMessage());
		}
	}
	
	public void loadAlertLog(String filename)
	{
		try
		{
			FileInputStream fin = new FileInputStream(filename);
	        ObjectInputStream ois = new ObjectInputStream(fin);
	        alertList = (List<Alert>) ois.readObject();
		}
		catch (Exception e)
		{			
			System.out.println(e.getMessage());
		}
	}
	
	public void saveHostLog(String filename)
	{
		try
		{
			FileOutputStream fout = new FileOutputStream(filename);
	        ObjectOutputStream oos = new ObjectOutputStream(fout);
	        oos.writeObject(hostList);
		}
		catch (Exception e)
		{			
			System.out.println(e.getMessage());
		}
	}
	
	public void loadHostLog(String filename)
	{
		try
		{
			FileInputStream fin = new FileInputStream(filename);
	        ObjectInputStream ois = new ObjectInputStream(fin);
	        hostList = (List<Host>) ois.readObject();
		}
		catch (Exception e)
		{			
			System.out.println(e.getMessage());
		}
	}
	
	public List<Alert> getAlertList()
	{
		return this.alertList;
	}
	
	public void setAlertList(List<Alert> alertList)
	{
		this.alertList = alertList;
	}
	
	public List<Host> getHostList()
	{
		return this.hostList;
	}
	
	public void setHostList(List<Host> hostList)
	{
		this.hostList = hostList;
	}
}
